package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Doctor;
import pojo.DoctorPage;
import pojo.OfficesPage;
import service.DoctorService;
import service.OfficesService;

/**
 * @author 李主席
 * @create 2020-12-11 9:08
 */
@Controller
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private OfficesService officesService;

    private Integer pageSize = 3;

    @RequestMapping("all_doctor")
    public String getAllUsers(Integer page, Model model) {
        Integer pid = page == null ? 1 : page;

        DoctorPage doctorPage = doctorService.getAllDoctors(pid, pageSize);
        OfficesPage allUsers = officesService.getAllUsers(pid, pageSize);
        //字符串users为输出到页面上的变量名称
        model.addAttribute("doctorPage", doctorPage);
        model.addAttribute("allUsers", allUsers);

        //页面路径=视图解析器前缀+页面名称+视图解析器后缀
        return "all_doctor";
    }

    @RequestMapping("go_add")
    public String gotoAddUser() {
        return "add_doctor";
    }

    //使用对象接收数据，提交的参数名称需要和对象属性对应
    @RequestMapping("add_doctor")
    public String addOneUser(Doctor doctor, Model model) {
        System.out.println(doctor);

        if (doctorService.addOneDoctor(doctor) > 0) {
            model.addAttribute("message", "医生新增成功");
        } else {
            model.addAttribute("message", "医生新增失败");
        }

        return "redirect:/all_doctor.action";
    }

    @RequestMapping("doctor_xiangqing")
    public String getUserByUid(Integer id, Model model) {
        Doctor isd = doctorService.getDoctorById(id);
        if (isd != null) {
            System.out.println("查询成功");
            model.addAttribute("doctor", isd);
        } else {
            System.out.println("查询失败");
        }
        return "set_doctor";
    }

    @RequestMapping("set_doctor")
    public String setusers(Doctor doctor, Model model) {
        Integer is = doctorService.setOneDoctor(doctor);
        if (is > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        return "redirect:/all_doctor.action";
    }

    @RequestMapping("del_doctor")
    public String delUserByUid(Integer id) {
        doctorService.delAllDoctors(id);
        //面试题：redirct与forward的区别
        //重定向到首页
        return "redirect:/all_doctor.action";
    }
}
